package org.example.judge_admin.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.judge_admin.service.AuthService;
import org.example.judge_admin.service.UserAuthService;
import org.example.judge_common.annotation.AuthAnnotation;
import org.example.judge_common.entity.ResponseResult;
import org.example.judge_common.util.MyUtils;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/auth")
@CrossOrigin
@Slf4j
public class AuthController {

    @Qualifier("AuthServiceImpl")
    @Autowired
    private AuthService authService;

    @Qualifier("UserAuthServiceImpl")
    @Autowired
    private UserAuthService userAuthService;
    @PostMapping("/list")
    @AuthAnnotation(Auth.ADMINISTRATOR)
    public ResponseResult list(String userId,Integer status){

        if(MyUtils.isEmpty(userId)&&status==null){  //查询全部
            return ResponseResult.success().setData(authService.list());
        }
//        if(status!=null){ //保留权限
//            List<Auth> auths = authService.list(new QueryWrapper<Auth>().eq("status",status));
//            return ResponseResult.success().setData(auths);
//        }
        List<Auth> auths = authService.listByUserId(userId,status,true);
        if(auths==null||auths.size()==0)return ResponseResult.NotData("查无权限数据");
        return ResponseResult.success().setData(auths);
    }

    @PostMapping("/save")
    @AuthAnnotation({Auth.ROOT,"auth_save"})
    public ResponseResult save(@RequestBody Auth auth){
        if(authService.getAuthByCode(auth.getCode())!=null)
            return ResponseResult.fail("已存在的权限");

        authService.save(auth);
        return ResponseResult.success();
    }
    @PutMapping("/update")
    @AuthAnnotation({Auth.ROOT,"auth_update"})
    public ResponseResult updateById(@RequestBody Auth auth){
        Auth authByCode = authService.getById(auth.getId());
        if(authByCode!=null&& !authByCode.getId().equals(auth.getId())){
            return ResponseResult.fail("已存在的权限");
        }
        ResponseResult res = ResponseResult.success();
        if (authByCode.getStatus()==2&&!auth.getCode().equals(authByCode.getCode())) {
            //保留权限名称不能更改
            auth.setCode(authByCode.getCode());
            return res = ResponseResult.NotData("此保留权限不可修改编码");
        }
        if(Auth.isKeep(authByCode.getCode())&&auth.getStatus()!=2){ //此保留权限status无法更改
            auth.setStatus(2);
            return res = ResponseResult.NotData("此权限不可修改状态");
        }
        auth.setUpdateTime(new Date());
        authService.updateById(auth);
        return res;
    }
    @DeleteMapping("/delete/{id}")
    @AuthAnnotation({Auth.ROOT})
    public ResponseResult deleteById(@PathVariable("id") String auth_id){
        Auth byId = authService.getById(auth_id);
        if(byId==null)return ResponseResult.fail("不存在的权限");
        if(Auth.isKeep(byId.getCode())||byId.getStatus()==2){  //保留权限不可删除
            return ResponseResult.fail("不可删除保留权限");
        }
        authService.removeById(auth_id);
        return ResponseResult.success();
    }

    /**
     * 为用户分配权限
     * @param userId
     * @param auths
     * @param status 只修改status相等的
     * @return
     */
    @PostMapping("/distribution/{id}")
    @AuthAnnotation({Auth.ROOT})
    public ResponseResult distribution(@PathVariable("id") String userId, String[] auths,@RequestParam Integer status){

        userAuthService.updateAuthByUserId(userId, auths==null?new ArrayList<>():new ArrayList<>(Arrays.asList(auths)),status);
        return ResponseResult.success();
    }


}
